local hx__renfeng = fk.CreateSkill {
  name = "hx__renfeng",
}

local HX = require "packages.konglong.hxfs2"

local spec = {
  on_cost = Util.TrueFunc,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local choices = {"#hx__renfeng_slash"}
    if table.find(player:getCardIds("h"), function (id)
      return Fk:getCardById(id).trueName == "slash"
    end) then
      table.insert(choices, "#hx__renfeng_trick")
    end
    local choice = room:askToChoice(player, {
      skill_name = hx__renfeng.name,
      choices = choices,
      cancelable = true,
    })
    if choice == "#hx__renfeng_trick" then
      player:broadcastSkillInvoke(hx__renfeng.name)
      player.room:doAnimate("InvokeSkill", {
        name = "hx__renfeng",
        player = player.id,
        skill_type = "special",
      })
      if #room:askToDiscard(player,{
          min_num = 1,
          max_num = 1,
          pattern = "slash",
          prompt = "请弃置一张【杀】并失去1点体力，从牌堆中随机获得一张伤害类锦囊牌",
          skill_name = hx__renfeng.name,
        }) ~= 0 then
        room:loseHp(player, 1, hx__renfeng.name)
        local cards = {}
        for i = 1, #room.draw_pile, 1 do
          local card = Fk:getCardById(room.draw_pile[i])
          if card:isCommonTrick() and card.is_damage_card then
            table.insertIfNeed(cards, card)
          end
        end
        if #cards > 0 then
          local getcard = cards[math.random(1, #cards)]
          room:obtainCard(player, getcard, true, fk.ReasonPrey, player, hx__renfeng.name)
        end
      end
    else
      player:broadcastSkillInvoke(hx__renfeng.name)
      player.room:doAnimate("InvokeSkill", {
        name = "hx__renfeng",
        player = player.id,
        skill_type = "offensive",
      })
      local judge = {
        who = player,
        reason = hx__renfeng.name,
        pattern = ".|.",
      }
      room:judge(judge)
      if judge.card.type == Card.TypeTrick then
        HX.GetCardFromPile(room, player,"slash",hx__renfeng.name,{"@@hx__renfeng_notimes-inhand",1})
      elseif judge.card.type == Card.TypeBasic then
        HX.GetCardFromPile(room, player,"slash",hx__renfeng.name,{"@@hx__renfeng_damage-inhand",1})
      elseif judge.card.type == Card.TypeEquip then
        HX.GetCardFromPile(room, player,"slash",hx__renfeng.name,{"@@hx__renfeng_extra-inhand",1})
      end
    end
  end,
}

hx__renfeng:addEffect(fk.GameStart, {
  can_trigger = function(self, event, target, player, data)
    return player:hasSkill(hx__renfeng.name)
  end,
  on_cost = function(self, event, target, player, data)
    local x = #player.room.alive_players
    if player.room:askToSkillInvoke(player, {
      skill_name = hx__renfeng.name,
      prompt = "刃锋：是否令场上的"..x.."名角色依次从牌堆中获得并使用一张武器牌？",
    }) then
      return true
    end
    return false
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    room:doIndicate(player, room.alive_players)
    local temp = player.next
    local players = {}
    while temp ~= player do
      if not temp.dead then
        table.insert(players, temp)
      end
      temp = temp.next
    end
    table.insert(players, player)
    for _, p in ipairs(players) do
      if not p.dead then
        local cards = {}
        for i = 1, #room.draw_pile, 1 do
          local card = Fk:getCardById(room.draw_pile[i])
          if card.sub_type == Card.SubtypeWeapon and p:canUse(card) and not p:prohibitUse(card) then
            table.insertIfNeed(cards, card)
          end
        end
        if #cards > 0 then
          local Weapon = cards[math.random(1, #cards)]
          room:useCard{
            from = p,
            tos = {p},
            card = Weapon,
          }
        end
      end
    end
  end,
})

hx__renfeng:addEffect(fk.Damage, {
  mute = true,
  trigger_times = function(self, event, target, player, data)
    return data.damage
  end,
  can_trigger = function(self, event, target, player, data)
    if target == player and player:hasSkill(hx__renfeng.name) then
      return #data.to:getEquipments(Card.SubtypeWeapon) > 0 or #player:getEquipments(Card.SubtypeWeapon) > 0
    end
  end,
  on_cost = spec.on_cost,
  on_use = spec.on_use,
})

hx__renfeng:addEffect(fk.Damaged, {
  mute = true,
  trigger_times = function(self, event, target, player, data)
    return data.damage
  end,
  can_trigger = function(self, event, target, player, data)
    if target == player and player:hasSkill(hx__renfeng.name) then
      return data.from and #data.from:getEquipments(Card.SubtypeWeapon) > 0 or #player:getEquipments(Card.SubtypeWeapon) > 0
    end
  end,
  on_cost = spec.on_cost,
  on_use = spec.on_use,
})

hx__renfeng:addEffect("targetmod", {
  bypass_times = function(self, player, skill, scope, card, to)
    return card and card:getMark("@@hx__renfeng_notimes-inhand") > 0
  end,
})


hx__renfeng:addEffect(fk.PreCardUse, {
  can_refresh = function (self, event, target, player, data)
    return target == player and data.card:getMark("@@hx__renfeng_notimes-inhand") > 0
  end,
  on_refresh = function (self, event, target, player, data)
    data.extraUse = true
  end,
})

hx__renfeng:addEffect(fk.PreCardUse, {
  mute = true,
  is_delay_effect = true,
  anim_type = "offensive",
  can_trigger = function(self, event, target, player, data)
    if target == player and player:hasSkill(hx__renfeng.name, true) and data.card then
      return data.card:getMark("@@hx__renfeng_damage-inhand") > 0 or data.card:getMark("@@hx__renfeng_extra-inhand") > 0
    end
  end,
  on_use = function (self, event, target, player, data)
    if data.card:getMark("@@hx__renfeng_extra-inhand") > 0 then
      data.additionalEffect = (data.additionalEffect or 0) + 1
    elseif data.card:getMark("@@hx__renfeng_damage-inhand") > 0 then
      data.additionalDamage = (data.additionalDamage or 0) + 1
    end
  end,
})


return hx__renfeng
